﻿using System;
using System.Linq;
using FluentNHibernate;
using FluentNHibernate.Cfg;
using MedicalCare.DbLayer.Adaptors;
using MedicalCare.EntityDefinitions.Entities;
using MedicalCare.Messages;
using MedicalCare.Translator;
using MedicalCare.Validation;
using NHibernate;
using StructureMap;

namespace MedicalCare.IoCSetup
{
    public static class BootStrap
    {
        public static void Init()
        {
            ObjectFactory.Initialize(x =>
                {
                    //x.For<iHPFSProductMaster>().Use<HPFSProductMaster.HPFSProductMaster>().Named("HPFSProductMaster");

                    //// FOD x.For<Translator.iTranslator>().Use<SalesIDSOpportunityToOpportunityMessage>();
                    //x.For<LMS_CRM.iLMS_CRM>().Use<LMS_CRM.LMS_CRM>();
                    //x.For<iMDCP>().Use<MDCP.MDCP>();
                    //x.For<iLMSValidator>().Use<OpportunityUpdateValidator>().Named("OpportunityUpdateValidator");
                    //x.For<APreValidator>().Use<PreValidator>();
                    //x.For<IPresaleResultValidator>().Use<PresaleResultValidator>();
                    //x.For<iHPFSProductMaster>().Use<HPFSProductMaster.HPFSProductMaster>().Named("HPFSProductMaster");

                    x.For<IScheduleAdaptor>().Use<ScheduleAdaptor>();
                    x.For<IUserInfoToUserMessage>().Use<UserInfoToUserMessage>();
                    x.For<IFamilyListToScheduleList>().Use<FamilyListToScheduleList>();

                    x.For<IUserLoginValidator>().Use<UserLoginValidator>();

                    x.For<IUserAdaptor>().Use<UserAdaptor>();
                    x.For<IBloodPressureAdaptor>().Use<BloodPressureAdaptor>();
                    x.For<IEquipmentsAdaptor>().Use<EquipmentsAdaptor>();
                    x.For<IEquipmentTypeAdaptor>().Use<EquipmentTypeAdaptor>();
                    x.For<IStandardValueAdaptor>().Use<StandardValueAdaptor>();
                    x.For<IFamilyAdaptor>().Use<FamilyAdaptor>();


                    x.AddRegistry(new MedicalNHibernateRegistry()); // For Bem
                

                    //x.For<IOpportunityXmlAdaptor>().Use<OpportunityXmlAdaptor>().Ctor<ISession>().Is(a => a.TheInstanceNamed("BEMSession"));
               
                }
            );
        }

        public static FluentMappingsContainer AddFromAssemblyOf<T>(this FluentMappingsContainer mappings, Predicate<Type> where)
        {
            if (where == null)
                return mappings.AddFromAssemblyOf<T>();

            var mappingClasses = typeof(T).Assembly.GetExportedTypes()
                .Where(x => typeof(IMappingProvider).IsAssignableFrom(x)
                    && where(x));

            foreach (var type in mappingClasses)
            {
                mappings.Add(type);
            }

            return mappings;
        }
    }
}